import java.awt.event.*;
import java.awt.*;
import javax.swing.*;;
import java.util.*;

class ManageUser extends JDialog implements ActionListener
{

    String username = "db2admin"; // Need you database Username and password
    String password = "crossbow";
    String database = "RENTMOV";

	Vector memberList;
	Vector mshipList;
	JList list;
	JButton editUser;
	JButton addUser;
	JButton delete;
	JScrollPane scroll;
	JPanel mainPanel;


	ManageUser(){

		Container c;
		c = getContentPane();

		mainPanel = new JPanel(new BorderLayout());
		scroll = new JScrollPane(getUserList());
		mainPanel.add(scroll, BorderLayout.EAST);
		JPanel westPanel = new JPanel();
		westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));

		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);
		editUser = new JButton("Edit User");
		editUser.setAlignmentX(Component.CENTER_ALIGNMENT);
		editUser.addActionListener(this);
		editUser.setActionCommand("edit");
		westPanel.add(editUser);
		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);

		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);
		addUser = new JButton("Add User");
		addUser.setAlignmentX(Component.CENTER_ALIGNMENT);
		addUser.addActionListener(this);
		addUser.setActionCommand("addUser");
		westPanel.add(addUser);
		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);

		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);
		delete = new JButton("Delete");
		delete.setAlignmentX(Component.CENTER_ALIGNMENT);
		delete.addActionListener(this);
		delete.setActionCommand("delete");
		westPanel.add(delete);
		westPanel.add(Box.createRigidArea(new Dimension(0,50)), BorderLayout.WEST);

		mainPanel.add(westPanel, BorderLayout.WEST);

		c.add(mainPanel);

        Toolkit tk;
        Dimension d;
        tk = Toolkit.getDefaultToolkit();
        d = tk.getScreenSize();
        setSize(400,400);
        setLocation((d.width/2)-200, (d.height/2)-200);
        setVisible(true);
	}

	private JList getUserList(){

        DBQTable dbq = new DBQTable(database);
		JScrollPane scroll;
		Container c = getContentPane();

		try{

			Vector userNames = dbq.getVector(username,password,"Select M.MNAME from Members M ORDER BY M.mship#");
			memberList = mshipList;
			list = new JList(userNames);
			mshipList = dbq.getVector(username,password,"Select M.MSHIP# from Members M ORDER BY M.mship#");
			return list;

		}catch (Exception x){
				JOptionPane.showMessageDialog(null,
				    "Error getting user Information",
				    "Error",
    				JOptionPane.ERROR_MESSAGE);
		}
		return null;
	}

	public void actionPerformed(ActionEvent e){

		if(e.getActionCommand().equals("edit")){

			int mshipNum = (Integer) mshipList.elementAt(list.getSelectedIndex());
			System.out.println("Member ID: " + mshipNum);
			new UserEditor(mshipNum, this);

		}else if(e.getActionCommand().equals("addUser")){
			new UserEditor(-1, this);
		}else if(e.getActionCommand().equals("delete")){

			int mshipNum = (Integer) mshipList.elementAt(list.getSelectedIndex());
			System.out.println("Member ID: " + mshipNum);

			DBQTable dbq = new DBQTable(database);

			try{

				dbq.update(username,password,"DELETE FROM HISTORY WHERE mship#=" + mshipNum);
				dbq.update(username,password,"DELETE FROM MEMBERS WHERE mship#=" + mshipNum);
				JOptionPane.showMessageDialog(null,
				    "Member removed\nmship#: " + mshipNum,
				    "Deletion Processed",
    				JOptionPane.PLAIN_MESSAGE);

			}catch (Exception x){
					JOptionPane.showMessageDialog(null,
						"Error getting user Information",
						"Error",
						JOptionPane.ERROR_MESSAGE);
			}

		}else{
			System.out.println("No Action Command Found");
		}
		reloadList();
	}

	public void reloadList(){
		mainPanel.remove(scroll);
		scroll = new JScrollPane(getUserList());
		mainPanel.add(scroll, BorderLayout.EAST);
		scroll.repaint();
		mainPanel.validate();
		mainPanel.repaint();
	}

}